package pers.myz.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import pers.myz.common.QueryPageParam;
import pers.myz.common.Result;
import pers.myz.entity.SnsPointsRule;
import pers.myz.entity.SnsTest;
import pers.myz.service.SnsPointsRuleService;
import pers.myz.service.SnsTestService;

import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lzw
 * @since 2023-07-01
 */
@RestController
@RequestMapping("/sns-points-rule")
public class SnsPointsRuleController {
    @Autowired
    private SnsPointsRuleService snsPointsRuleService;
    //新增
    @PostMapping("/save")
    public Result save(@RequestBody SnsPointsRule snsPointsRule){return snsPointsRuleService.save(snsPointsRule)?Result.suc():Result.fail();}
    //更新
    @PostMapping("/update")
    public Result update(@RequestBody SnsPointsRule snsPointsRule){return snsPointsRuleService.updateById(snsPointsRule)?Result.suc():Result.fail();}
    //删除
    @GetMapping("/del")
    public Result del(@RequestParam String id){return snsPointsRuleService.removeById(id)?Result.suc():Result.fail();}

    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String function = (String)param.get("function");
        String pointsAmount = (String)param.get("pointsAmount");


        IPage<SnsPointsRule> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<SnsPointsRule> lambdaQueryWrapper = new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(function) && !"null".equals(function)){
            lambdaQueryWrapper.like(SnsPointsRule::getFunction,function);
        }
        if(StringUtils.isNotBlank(pointsAmount) && !"null".equals(pointsAmount)){
            lambdaQueryWrapper.eq(SnsPointsRule::getPointsAmount,pointsAmount);
        }

        IPage result = snsPointsRuleService.pageC(page,lambdaQueryWrapper);
        return Result.suc(result.getRecords(),result.getTotal());
    }


}
